package com.lojzes.minwebserver;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Properties;

public class Server {
	private int port = 0;
	private String webRoot = "";
	private ServerSocket serverSocket = null;
	{
		try {
			Properties properties = new Properties();
			InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties");
			properties.load(in);
			port = Integer.parseInt((String) properties.get("port"));
		    webRoot = (String)properties.get("web_root");
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public void serverStart() {
		try {
			serverSocket = new ServerSocket(port);
			while (true) {
				System.out.println("server started...");
				Socket socket = serverSocket.accept();
				new Processor(socket,webRoot).start();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				serverSocket.close();
			} catch (IOException e) {
 				e.printStackTrace();
			}
		}

	}

	public static void main(String[] args) {
		new Server().serverStart();
	}
}
